import tkinter
import tkinter.filedialog
import zipfile
import os
import tkinter.messagebox
import rarfile
import py7zr 

class Yasuo:
    def __init__(self):
        self.root = tkinter.Tk()
        self.root.minsize(400, 300)
        self.root.title('答辩压缩器')
        self.create_widgets()
        self.root.mainloop()

    def create_widgets(self):
        # 添加文件按钮
        self.btn_add = tkinter.Button(self.root, text='添加文件', command=self.press_add)
        self.btn_add.grid(row=0, column=0, padx=10, pady=10)

        # 压缩文件按钮
        self.btn_ys = tkinter.Button(self.root, text='压缩文件', command=self.press_ys)
        self.btn_ys.grid(row=0, column=1, padx=10, pady=10)

        # 解压文件按钮
        self.btn_jy = tkinter.Button(self.root, text='解压文件', command=self.press_jy)
        self.btn_jy.grid(row=0, column=2, padx=10, pady=10)

        # 显示文件列表的文本框
        self.file_list_label = tkinter.Label(self.root, text='文件列表:')
        self.file_list_label.grid(row=1, column=0, columnspan=3, pady=5)

        self.file_listbox = tkinter.Listbox(self.root, width=50, height=10)
        self.file_listbox.grid(row=2, column=0, columnspan=3, padx=10, pady=5)

        # 状态栏
        self.status_var = tkinter.StringVar()
        self.status_var.set('请添加文件')
        self.status_label = tkinter.Label(self.root, textvariable=self.status_var, bg='lightgrey', width=50)
        self.status_label.grid(row=3, column=0, columnspan=3, pady=5)

    def press_add(self):
        # 添加文件或文件夹
        paths = tkinter.filedialog.askopenfilenames(initialdir='/', title='选择文件或文件夹', multiple=True)
        for path in paths:
            self.file_listbox.insert(tkinter.END, path)
        self.status_var.set(f'已添加 {len(paths)} 个文件或文件夹')

    def press_ys(self):
        # 压缩文件或文件夹
        if self.file_listbox.size() > 0:
            zip_path = tkinter.filedialog.asksaveasfilename(defaultextension='.zip', filetypes=[('Zip files', '*.zip'), ('7z files', '*.7z')])
            if zip_path:
                if zip_path.endswith('.zip'):
                    with zipfile.ZipFile(zip_path, 'w') as zf:
                        for idx in range(self.file_listbox.size()):
                            file_path = self.file_listbox.get(idx)
                            if os.path.isfile(file_path):
                                zf.write(file_path, os.path.basename(file_path))
                            elif os.path.isdir(file_path):
                                base_folder_name = os.path.basename(file_path)
                                for root, _, files in os.walk(file_path):
                                    for file in files:
                                        file_to_zip = os.path.join(root, file)
                                        zf.write(file_to_zip, os.path.relpath(file_to_zip, file_path))
                elif zip_path.endswith('.7z'):
                    with py7zr.SevenZipFile(zip_path, 'w') as szf:
                        for idx in range(self.file_listbox.size()):
                            file_path = self.file_listbox.get(idx)
                            szf.write(file_path)
                tkinter.messagebox.showinfo('压缩成功', f'成功压缩到 {zip_path}')
        else:
            tkinter.messagebox.showinfo('提示', '请先添加文件或文件夹')

    def press_jy(self):
        # 解压文件
        zip_path = tkinter.filedialog.askopenfilename(filetypes=[('Zip files', '*.zip'), ('RAR files', '*.rar'), ('7z files', '*.7z')])
        if zip_path:
            extract_path = tkinter.filedialog.askdirectory()
            if zip_path.endswith('.zip'):
                with zipfile.ZipFile(zip_path, 'r') as zf:
                    zf.extractall(extract_path)
            elif zip_path.endswith('.rar'):
                with rarfile.RarFile(zip_path, 'r') as rf:
                    rf.extractall(extract_path)
            elif zip_path.endswith('.7z'):
                with py7zr.SevenZipFile(zip_path, 'r') as szf:
                    szf.extractall(path=extract_path)
            tkinter.messagebox.showinfo('解压成功', f'成功解压到 {extract_path}')
        else:
            tkinter.messagebox.showinfo('提示', '请选择要解压的压缩文件')

# 启动应用程序
if __name__ == '__main__':
    yasuo_app = Yasuo()
